﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Financing.Model;
using Honestysoft.BaseObject;
using System.Data;
using Honestysoft.Financing.WebUI.Module.Audit;
using System.IO;

namespace Honestysoft.Financing.WebUI.Module.Reports
{
    public partial class BatchReportPrint : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request.QueryString["BatchID"];
            string docNo = Request.QueryString["DocNo"];
            string batchNo= Request.QueryString["BatchNo"];
            string dir =  "Reports/"+batchNo;
            BaseBusinessLogic<Bu_BatchReport> bll = new BaseBusinessLogic<Bu_BatchReport>(Globals.CurrentConnStringName);
            Bu_BatchReport report = bll.GetEntity(id);
            report.DocNo = docNo;
            report.DocDate = Convert.ToDateTime(Request.QueryString["DocDate"]);
            bll.UpdateEntity(report);
            QueryParameters query = new QueryParameters();
            query.Add("BatchID", report.ID, QueryParameters.OperatorType.Equal);
            OrderParameters order = new OrderParameters();
            order.Add("Code");
            DataTable dt = new BaseBusinessLogic<Bu_AuditTable>(Globals.CurrentConnStringName).GetTable(query, order);
            List<string> lsOwner = new List<string>();
            List<string> lsProject = new List<string>();
            List<string> lsContract = new List<string>();
            decimal sumDocRec = 0, sumAmountS = 0;
            DateTime? minTime = null, maxTime = null;
            string ownerString = "";
            foreach (DataRow dr in dt.Rows)
            {
                if (minTime == null)
                {
                    minTime = Convert.ToDateTime(dr["DocRecTime"]);
                }
                else
                {
                    if (minTime > Convert.ToDateTime(dr["DocRecTime"]))
                    {
                        minTime = Convert.ToDateTime(dr["DocRecTime"]);
                    }
                }
                if (maxTime == null)
                {
                    maxTime = Convert.ToDateTime(dr["DocRecTime"]);
                }
                else
                {
                    if (maxTime < Convert.ToDateTime(dr["DocRecTime"]))
                    {
                        maxTime = Convert.ToDateTime(dr["DocRecTime"]);
                    }
                }
                if (!lsOwner.Contains(dr["OwnerName"].ToString())) { lsOwner.Add(dr["OwnerName"].ToString()); }
                if (!lsProject.Contains(dr["ProjectID"].ToString())) { lsProject.Add(dr["ProjectID"].ToString()); }
                if (!lsContract.Contains(dr["ContractID"].ToString())) { lsContract.Add(dr["ContractID"].ToString()); }
                sumDocRec += Convert.ToDecimal(dr["Amount_DocRec"]);
                sumAmountS += Convert.ToDecimal(dr["Amount_S"]);
            }
            foreach (string s in lsOwner)
            {
                if (ownerString != "") { ownerString += "、"; }
                ownerString += s;
            }
            Honestysoft.BaseLibrary.Common.WordHelper wordHelper = new BaseLibrary.Common.WordHelper();
            string fileTemplate = Server.MapPath(Globals.Home+"Module/Reports/Template/第X批审核工作的情况报告.doc");
            wordHelper.CreateNewWordDocument(fileTemplate);
            wordHelper.Replace("bm1", docNo);
            wordHelper.Replace("bm2", ChineseNum.GetChineseNum(Request.QueryString["BatchNo"]));
            if (minTime.Value.ToString("yyyyMMdd") == maxTime.Value.ToString("yyyyMMdd"))
            {
                wordHelper.Replace("bm3", minTime.Value.ToString("M月d日") );
            }
            else
            {
                wordHelper.Replace("bm3", minTime.Value.ToString("M月d日") + "至" + maxTime.Value.ToString("M月d日"));
            }
            wordHelper.Replace("bm4", ownerString+ ChineseNum.GetChineseNum(lsOwner.Count.ToString()));
            //wordHelper.Replace("bm5",);
            wordHelper.Replace("bm6", lsProject.Count.ToString());
            wordHelper.Replace("bm7", lsContract.Count.ToString());
            wordHelper.Replace("bm8", dt.Rows.Count.ToString());
            wordHelper.Replace("bm9", string.Format("{0:N}", sumDocRec));
            wordHelper.Replace("bm10", string.Format("{0:N}", sumAmountS));
            wordHelper.Replace("bm11", lsProject.Count.ToString());
            wordHelper.Replace("bm12", lsContract.Count.ToString());
            wordHelper.Replace("bm13", dt.Rows.Count.ToString());
            wordHelper.Replace("bm14", ChineseNum.GetChineseNum(Request.QueryString["BatchNo"]));
            wordHelper.Replace("bm15", report.DocDate.ToString("yyyy年M月d日"));
            wordHelper.Replace("bm16", report.DocDate.ToString("yyyy年M月d日"));
            string fileName= Server.MapPath( Globals.Home+report.DocUrl);
            if (!Directory.Exists(Server.MapPath(Globals.Home + dir)))
            {
                Directory.CreateDirectory(Server.MapPath(Globals.Home + dir));
            }
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            wordHelper.SaveAs(fileName);
            wordHelper.Close();
            Response.Redirect(Globals.Home + report.DocUrl);
        }
    }
}